package com.hcj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hcj.mapper.CustomerMapper;
import com.hcj.pojo.Customer;
import com.hcj.service.CustomerService;
import com.hcj.util.MyResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerMapper mapper;

    @Override
    public boolean login(Customer customer) {
        //查询有没有和customer中的用户名、密码一致的用户
        LambdaQueryWrapper<Customer> eq = new LambdaQueryWrapper<Customer>()
                .eq(Customer::getUsername, customer.getUsername())
                .eq(Customer::getPassword, customer.getPassword());
        Customer customer1 = mapper.selectOne(eq);
        return customer1!=null;
    }

    @Override
    public MyResponse register(Customer customer) {
        //1.判断用户名是否重复
        Customer customer1 = mapper.selectOne(new LambdaQueryWrapper<Customer>().eq(Customer::getUsername, customer.getUsername()));
        if (customer1 ==null)//用户名不存在
        {
            int insert = mapper.insert(customer);
            if(insert==1){
                return new MyResponse("100","注册成功");
            }else {
                return new MyResponse("200","数据库插入失败");
            }

        }
        return new MyResponse("200","用户名重复");

        //2.
    }

    @Override
    public MyResponse delete(Customer customer) {
        //根据customer的customer name来删除用户
        LambdaQueryWrapper<Customer> eq = new LambdaQueryWrapper<Customer>().eq(Customer::getUsername, customer.getUsername());
        int delete = mapper.delete(eq);
        if (delete==1){
            return new MyResponse("100","删除成功");
        }
        else return new MyResponse("200","存在该用户未删除的订单，删除失败!");
    }

    /**
     * 模糊查询
     * @param username
     * @return
     */
    @Override
    public List<Customer> selectByName(String username) {
        List<Customer> customer = mapper.selectList(new LambdaQueryWrapper<Customer>().like(Customer::getUsername, "%" + username + "%"));

        return customer;
    }

    @Override
    public Page<Customer> selectAllPaging(Integer currentPage, Integer pageSize) {
        Page<Customer> customerPage = new Page<>();
        customerPage.setCurrent(currentPage);
        customerPage.setSize(pageSize);
        Page<Customer> customerPage1 = mapper.selectPage(customerPage, null);
        return customerPage1;
    }
    @Override
    public MyResponse update(Customer customer){
        //1.判断用户是否存在
        Customer customer1 = mapper.selectOne(new LambdaQueryWrapper<Customer>().eq(Customer::getId, customer.getId()));
        if (customer1==null){
            return new MyResponse("200","用户不存在");
        }
        int i = mapper.updateById(customer);
        if (i==1){
            return new MyResponse("100","修改成功");
        }
        else return new  MyResponse("200","修改异常！");
    }
    @Override
    public Customer selectOneByName(Customer customer){
        return mapper.selectOne(new LambdaQueryWrapper<Customer>().eq(Customer::getUsername,customer.getUsername()));
    }
    @Override
    public  int add(Customer customer){
        return mapper.insert(customer);
    }
}
